package cn.fancy.sdk.permission;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import cn.fancy.sdk.common.utils.ResUtil;
import cn.fancy.sdk.pay.WebViewActivity;

/**
 * Created by zhaojh on date 2023/12/5
 */
public class InitPermissionActivity extends Activity {
    public static final int REQ_AGREE = 1;
    
    public static void start(Activity activity, int requestCode) {
        Intent intent = new Intent(activity, InitPermissionActivity.class);
        activity.startActivityForResult(intent, requestCode);
    }
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(ResUtil.getLayoutId(this, "guild_init_permission_activity"));
        
        TextView tvContext = (TextView) this.findViewById(ResUtil.getId(this, "tv_content"));
    
        tvContext.setMovementMethod(LinkMovementMethod.getInstance());
    
        SpannableString ss = new SpannableString("我们非常重视您的个人信息和隐私保护，在亲爱的玩家您进入游戏前，请您务必谨慎阅读[用户协议]和[隐私政策],并请点击“同意”开始使用我们的服务,我们将严格按照您同意的各项条款使用您的个人信息,以便为您提供更好的服务。");
       
        ss.setSpan(new ForegroundColorSpan(Color.BLUE), 39, 45,  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                Log.d("InitPermissionActivity", "InitPermissionActivity Click");
                String url = "https://game.daimiaohudong.cn/mobile/float/game_agreement";
                WebViewActivity.start(InitPermissionActivity.this, url, "用户协议");
            }
        }, 39, 45,  Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        
        ss.setSpan(new ForegroundColorSpan(Color.BLUE), 46, 51,  Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        ss.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                String url = "https://game.daimiaohudong.cn/mobile/float/game_privacy";
                WebViewActivity.start(InitPermissionActivity.this, url, "隐私政策");
            }
        }, 46, 51,  Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
        
        tvContext.setText(ss);
    
        
        TextView tv_agree = (TextView) this.findViewById(ResUtil.getId(this, "tv_agree"));
        tv_agree.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                intent.putExtra("result", REQ_AGREE);
                InitPermissionActivity.this.setResult(RESULT_OK, intent);
                InitPermissionActivity.this.finish();
            }
        });
    
    
        TextView tv_reject = (TextView) this.findViewById(ResUtil.getId(this, "tv_reject"));
        tv_reject.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(InitPermissionActivity.this, "不同意无法进入游戏",Toast.LENGTH_SHORT).show();
            }
        });
    }
    
    private long firstTime = 0;
    
    @Override
    public void onBackPressed() {
        long secondTime = System.currentTimeMillis();
        if (secondTime - firstTime < 2000) {
//            android.os.Process.killProcess(android.os.Process.myPid());
//            System.exit(1);
    
            ActivityManager am = (ActivityManager)getSystemService (Context.ACTIVITY_SERVICE);
            am.restartPackage(getPackageName());
        } else {
            Toast.makeText(InitPermissionActivity.this, "再点一次退出程序", Toast.LENGTH_SHORT).show();
            firstTime = System.currentTimeMillis();
        }
    }
}
